<?php
/**
 * @version $Id: view.html.php 48 2011-09-28 06:47:49Z lnmtrung $
 */
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );

class WrdViewTransaction extends WrdView{
    
    function display($tpl = null) {
        
        $this->setModel($this->getModel('transaction'), true);
        $fnc = 'display'.ucfirst($this->getLayout());
        if (is_callable(array(&$this, $fnc)))
            $this->$fnc($tpl);
        else
            parent::display($tpl);
    }

    function displayDefault($tpl = null){
        
        $mainframe = &JFactory::getApplication();
        $context = JRequest::getCmd('option');
        $view = JRequest::getCmd('view');
        
        $document = & JFactory::getDocument();
        $document->setTitle( JText::_('Transaction Manager') );

        JToolBarHelper::title(JText::_('Transaction Manager'),'transaction.png');
         JToolBarHelper::addNew();
         JToolBarHelper::publish();
         JToolBarHelper::unpublish();
        JToolBarHelper::deleteList();
        
        WrdHelper::addSubMenu('config',$view);
		$this->sidebar = JHtmlSidebar::render();
        
        $view = JRequest::getCmd('view');
        $filter_order             = $mainframe->getUserStateFromRequest( $context.$view.'filter_order_item','filter_order','id' );
        $filter_order_Dir         = $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir','filter_order_Dir', 'DESC' );
        
        $lists['order']         = $filter_order;
        $lists['order_Dir']     = $filter_order_Dir;
        
        $items                    = & $this->get( 'Data');
        $total                    = & $this->get( 'Total');
        $pagination             = & $this->get( 'Pagination' );

        $search                 = $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string' );
        $filter_user            = $mainframe->getUserStateFromRequest( $context.$view.'created_id','created_id',-1,'int' );

        $search                 = JString::strtolower( $search );

        $lists['search']         = $search;
        $lists['user']             = JHTML::_('wrd.listUser','created_id',$filter_user,'onchange="this.form.submit()"','status');
        
        $this->assignRef('search',        $search);
        $this->assignRef('lists',        $lists);
          $this->assignRef('items',        $items);
        $this->assignRef('pagination',    $pagination);
        
        parent::display($tpl);
      }
  
	function displayFormimport($tpl = null){
        $mainframe = &JFactory::getApplication();
        $context = JRequest::getCmd('option');
        $view = JRequest::getCmd('view');
        $user = JFactory::getUser();
        $id = JRequest::getVar('id');
        $detail = $this->getFile($id);
        $mainframe->setUserState('fileid', $id);
        JToolBarHelper::title(   JText::_( 'Transaction' ).': <small><small>[IMPORT]</small></small>','transaction.png' );
        JToolBarHelper::cancel( 'cancel', 'Close' );
        
        $db = JFactory::getDbo();
        $query = 'UPDATE #__wrd_file SER product_id = -1 WHERE id = '.$id;
        $db->setQuery($query);
        $db->query();
        
        $this->assignRef('data',$detail);
        parent::display($tpl);
    }
    
    public function displayForm(){
        $mainframe = &JFactory::getApplication();
        $context = JRequest::getCmd('option');
        $view = JRequest::getCmd('view');
        $user = JFactory::getUser();
        
        JToolBarHelper::title(   JText::_( 'Transaction' ).': <small><small>[UPLOAD]</small></small>','transaction.png' );
        JToolBarHelper::cancel( 'cancel', 'Close' );
        $this->assignRef('user', $user);
        parent::display();
    }
    
    public function displayFormconvert(){
        $mainframe = &JFactory::getApplication();
        $context = JRequest::getCmd('option');
        $view = JRequest::getCmd('view');
        $user = JFactory::getUser();
        $id = JRequest::getVar('id');
        $detail = $this->getFile($id);
        $mainframe->setUserState('fileid', $id);
        JToolBarHelper::title(   JText::_( 'Transaction' ).': <small><small>[IMPORT]</small></small>','transaction.png' );
        JToolBarHelper::cancel( 'cancel', 'Close' );
        
        $db = JFactory::getDbo();
        $query = 'UPDATE #__wrd_file SER product_id = -2 WHERE id = '.$id;
        $db->setQuery($query);
        $db->query();
        
        $this->assignRef('data',$detail);
        parent::display();
    }
    
    public function displayFormrevert(){
        parent::display();
    }
    
    public function getFile($id){
        $db = JFactory::getDbo();
        $query = 'SELECT * FROM #__wrd_file WHERE id = '.$id;
        $db->setQuery($query);
        $rs = $db->loadObjectList();
        return $rs;
    }
}           